//
//  HJSendVoiceView.m
//  KaiChat
//
//  Created by lx on 2023/10/27.
//

#import "HJRecordView.h"
#import "HJCircleProgress.h"
#import "HJRippleAnimationView.h"
#import <FLAnimatedImage.h>

@interface HJSendVoiceButton : UIView

@property (nonatomic, strong) UIImage *image;
@property (nonatomic, weak) UIButton *button;


@end

@implementation HJSendVoiceButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        UIView *contentView = [UIView addToView:self];
        contentView.backgroundColor = UIColor.whiteColor;
        contentView.layer.shadowColor = UIColor.blackColor.CGColor;
        contentView.layer.shadowOffset = CGSizeZero;
        contentView.layer.shadowRadius = 20;
        contentView.layer.shadowOpacity = 0.1;
        contentView.layer.cornerRadius = 28;
        [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.offset(0);
        }];
        
        UIButton *button = [UIButton addToView:contentView];
        button.userInteractionEnabled = NO;
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.offset(0);
        }];
        _button = button;
    }
    return self;
}

- (void)setImage:(UIImage *)image {
    _image = image;
    [_button setImage:image forState:UIControlStateNormal];
}

@end



@interface HJRecordView()

@property (nonatomic, strong) HJCircleProgress *circleProgress;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) FLAnimatedImageView *microphoneImageView;
@property (nonatomic, strong) NSArray *operationViews;
@property (nonatomic, weak) HJRippleAnimationView *rippleAnimationView;

@end

@implementation HJRecordView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.colors = @[[UIColor.whiteColor colorWithAlphaComponent:0], UIColor.whiteColor];
        [self setStartPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)];
        self.locations = @[@-0.3, @0.7];
        
        UIView *contentView = [UIView addToView:self];
        [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.offset(0);
            make.bottom.offset(0);
            make.height.offset(200);
        }];
        
        HJSendVoiceButton *cancalButton = [HJSendVoiceButton addToView:contentView];
        cancalButton.image = [UIImage imageNamed:@"voice_cancal"];
        [cancalButton addTapGesturesWithTarget:self action:@selector(didClickCancelButton)];
        [cancalButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(40);
            make.width.height.offset(56);
        }];
        
        UILabel *cancalLabel = [UILabel lableWithSuperView:contentView font:UIFontMake(14) color:UIColor.tj_868391Color title:@"Cancel" textAlignment:NSTextAlignmentLeft];
        [cancalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(cancalButton);
            make.top.equalTo(cancalButton.mas_bottom).offset(6);
        }];
        
        HJSendVoiceButton *sendButton = [HJSendVoiceButton addToView:contentView];
        sendButton.image = [UIImage imageNamed:@"voice_send"];
        [sendButton addTapGesturesWithTarget:self action:@selector(didClickSendButton)];
        [sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.right.inset(40);
            make.width.height.offset(56);
        }];
        
        UILabel *sendLabel = [UILabel lableWithSuperView:contentView font:UIFontMake(14) color:UIColor.tj_868391Color title:@"Send" textAlignment:NSTextAlignmentLeft];
        [sendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(sendButton);
            make.top.equalTo(sendButton.mas_bottom).offset(6);
        }];

        CGFloat circleProgressW = 118;
        CGFloat rippleAnimationViewW = 102;

        HJRippleAnimationView *rippleAnimationView = [[HJRippleAnimationView alloc] initWithFrame:CGRectMake(0, 0, rippleAnimationViewW, rippleAnimationViewW) animationType:HJAnimationTypeWithBackground];
        [contentView addSubview:rippleAnimationView];
        [rippleAnimationView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.centerX.offset(0);
            make.width.height.offset(circleProgressW);
        }];
        
        UIView *rippleView = [UIView addToView:contentView];
        rippleView.backgroundColor = UIColor.whiteColor;
        rippleView.addCornerRadius(rippleAnimationViewW * 0.5);
        [rippleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(rippleAnimationView);
            make.height.width.offset(rippleAnimationViewW);
        }];
        
        self.circleProgress = [[HJCircleProgress alloc] initWithFrame:CGRectMake(0, 0, circleProgressW, circleProgressW)];
        self.circleProgress.progress = 0;
        [contentView addSubview:self.circleProgress];
        [self.circleProgress mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(rippleView);
            make.width.height.offset(circleProgressW);
        }];
        
        UIView *placeholderView = [UIView addToView:contentView];
        [placeholderView addTapGesturesWithTarget:self action:@selector(tapRippleView)];
        [placeholderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(rippleAnimationView);
        }];
       
        FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"icon-Microphone.gif" withExtension:nil]]];
        FLAnimatedImageView *microphoneImageView = [[FLAnimatedImageView alloc] init];
        microphoneImageView.animatedImage = image;
        [self.circleProgress addSubview:microphoneImageView];
        [microphoneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(28);
            make.width.height.offset(30);
            make.centerX.offset(0);
        }];
        
        UILabel *timeLabel = [UILabel lableWithSuperView:self.circleProgress font:UIFontMake(12) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
        _timeLabel = timeLabel;
        [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.equalTo(microphoneImageView.mas_bottom).offset(10);
        }];
        
        _microphoneImageView = microphoneImageView;
        _rippleAnimationView = rippleAnimationView;

        _operationViews = @[cancalButton, cancalLabel, sendLabel, sendButton];
        
        [self showOperationViews:NO];
    }
    return self;
}

- (void)showOperationViews:(BOOL)show {
    for (UIView *view in self.operationViews) {
        view.hidden = !show;
    }
}

- (void)tapRippleView {
    [self stop];
    if ([self.delegate respondsToSelector:@selector(didClickMicrophone:)]) {
        [self.delegate didClickMicrophone:self];
    }
}

- (void)didClickCancelButton {
    [self stop];
    self.circleProgress.progress = 0;
    if ([self.delegate respondsToSelector:@selector(didClickCancel:)]) {
        [self.delegate didClickCancel:self];
    }
}

- (void)didClickSendButton {
    [self.microphoneImageView stopAnimating];
    if ([self.delegate respondsToSelector:@selector(didClickSend:)]) {
        [self.delegate didClickSend:self];
    }
}
 

- (void)setStatus:(TJRecordStatus)status {
    _status = status;
    
 
    
    switch (status) {
        case TJRecord_Status_Recording:
        {
//            _title.text = @"手指上滑，取消发送";
//            _title.backgroundColor = [UIColor clearColor];
            break;
        }
        case TJRecord_Status_Cancel:
        {
//            _title.text = @"松开手指，取消发送";
//            _title.backgroundColor = Record_Title_Background_Color;
            break;
        }
        case TJRecord_Status_TooShort:
        {
//            _title.text = @"说话时间太短";
//            _title.backgroundColor = [UIColor clearColor];
            break;
        }
        case TJRecord_Status_TooLong:
        {
//            _title.text = @"说话时间太长";
//            _title.backgroundColor = [UIColor clearColor];
            break;
        }
        default:
            break;
    }
}

- (void)setSecond:(NSTimeInterval)second {
    _second = second;
  
    NSString *currentSecond = [NSString stringWithFormat:@"%.f", second];
    _timeLabel.attributedText = [[NSString stringWithFormat:@"%@\" / %@", currentSecond, @"60"] attributedStringWithRangeString:[NSString stringWithFormat:@"%@\" /", currentSecond] rangeColor:UIColor.tj_333333Color color:UIColor.tj_BEBDCAColor];
    self.circleProgress.progress = second / 60.0;
}

- (void)stop {
    self.rippleAnimationView.hidden = YES;
    [self.microphoneImageView stopAnimating];
}

- (void)reset {
    self.rippleAnimationView.hidden = NO;
    self.circleProgress.progress = 0;
    [self.microphoneImageView stopAnimating];
    [self showOperationViews:NO];
}

- (void)dealloc {
    TJLog(@"%s", __func__);
}

@end
